<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form
      :inline="true"
      :model="query"
      class="demo-form-inline"
    >
      <el-form-item label="昵称">
        <el-input
          v-model="query.nickName"
          placeholder="昵称"
        />
      </el-form-item>
      <el-form-item label="登录名">
        <el-input
          v-model="query.userName"
          placeholder="登录名"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="search"
        >查询</el-button>
        <el-button
          type="primary"
          @click="handleAdd"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="nickName"
        label="昵称"
        width="180"
      />
      <el-table-column
        prop="userName"
        label="登录名"
        width="180"
      />
      <el-table-column
        prop="deptName"
        label="所属部门"
        width="180"
      />
      <el-table-column
        prop="age"
        label="年龄"
      />
      <el-table-column
        prop="gender"
        label="性别"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.gender === 1"
            type="success"
            disable-transitions
          >男</el-tag>
          <el-tag
            v-else
            type="danger"
            disable-transitions
          >女
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === 1"
            type="success"
            disable-transitions
          >启用</el-tag>
          <el-tag
            v-else
            type="danger"
            disable-transitions
          >禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
      />
      <el-table-column
        label="操作"
        align="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="query.total"
    />

    <!-- 新增、编辑用户 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
      >
        <el-form-item
          label="用户名"
          prop="userName"
          label-width="120px"
        >
          <el-input
            v-model="form.userName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="昵称"
          prop="nickName"
          label-width="120px"
        >
          <el-input
            v-model="form.nickName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="所属部门"
          label-width="120px"
        >
          <el-select
            v-model="form.deptId"
            placeholder="请选择所属部门"
          >
            <el-option
              v-for="item in departments"
              :key="item.id"
              :value="item.id"
              :label="item.deptName"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="手机"
          label-width="120px"
        >
          <el-input
            v-model="form.phone"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="年龄"
          label-width="120px"
        >
          <el-input
            v-model="form.age"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="邮箱"
          label-width="120px"
        >
          <el-input
            v-model="form.email"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="性别"
          label-width="120px"
        >
          <el-select
            v-model="form.gender"
            placeholder="请选择性别"
          >
            <el-option
              label="男"
              :value="1"
            />
            <el-option
              label="女"
              :value="2"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="状态"
          label-width="120px"
        >
          <el-select
            v-model="form.status"
            placeholder="请选择状态"
          >
            <el-option
              label="启用"
              :value="1"
            />
            <el-option
              label="禁用"
              :value="0"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-if="title === '新增'"
          type="primary"
          @click="add"
        >确 定</el-button>
        <el-button
          v-else-if="title === '编辑'"
          type="primary"
          @click="edit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { getPage, add, edit, getUser, del } from '@/api/user'
import { getList } from '@/api/department'

export default {
  data() {
    return {
      tableData: [],
      query: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
        nickName: '',
        userName: ''
      },
      dialogFormVisible: false,
      form: {},
      title: '',
      departments: [],
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        nickName: [{ required: true, message: '请输入昵称', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getPage()
  },
  methods: {
    getPage() {
      getPage(this.query).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },
    search() {
      this.getPage()
    },
    handleAdd() {
      this.title = '新增'
      this.form = {}
      this.getDepartments()
      this.dialogFormVisible = true
    },
    add() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          add(this.form).then((res) => {
            if (res.code === 0) {
              this.$message({
                message: '添加用户成功',
                type: 'success'
              })
              this.dialogFormVisible = false
              this.getPage()
            }
          })
        }
      })
    },
    handleEdit(row) {
      this.title = '编辑'
      this.form = {}
      getUser({ id: row.id }).then((res) => {
        this.form = res.data
      })
      this.getDepartments()
      this.dialogFormVisible = true
    },
    edit() {
      edit(this.form).then((res) => {
        if (res.code === 0) {
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.getPage()
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del({ id: row.id }).then((res) => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getPage()
            } else {
              this.$message({
                type: 'danger',
                message: '删除失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    getDepartments() {
      getList().then((res) => {
        this.departments = res.data
      })
    }
  }
}
</script>
